/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import app.transittool.model.Agency;

import trs.wmata.Bus;
import static trs.wmata.api.ApiXml.DIRECTION_NUM;
import static trs.wmata.api.ApiXml.DIRECTION_TEXT;
import static trs.wmata.api.ApiXml.MINUTES;
import static trs.wmata.api.ApiXml.NEXT_BUS_PREDICTION;
import static trs.wmata.api.ApiXml.ROUTE_ID;
import static trs.wmata.api.ApiXml.VEHICLE_ID;
import static trs.wmata.api.Util.toInt;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class NextBus extends ApiResponse {
    private Bus bus = null;
    private final Agency agency;
    private final ArrayList<Bus> buses = new ArrayList<Bus>();

    public NextBus(final Agency agency) {
        this.agency = agency;
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        if (NEXT_BUS_PREDICTION.equals(lName)) {
            bus = new Bus(agency);

            // } else if (DIRECTIONNUM.equals(lName)) {
            // } else if (DIRECTIONTEXT.equals(lName)) {
            // } else if (MINUTES.equals(lName)) {
            // } else if (NEXTBUSRESPONSE.equals(lName)) {
            // } else if (PREDICTIONS.equals(lName)) {
            // } else if (ROUTEID.equals(lName)) {
            // } else if (STOPNAME.equals(lName)) {
            // } else if (VEHICLEID.equals(lName)) {
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        if (NEXT_BUS_PREDICTION.equals(lName)) {
            if (bus != null) {
                buses.add(bus);
                bus = null;
            }
        } else if (bus != null) {
            final String str = buff.toString().trim();

            if (DIRECTION_NUM.equals(lName)) {
                bus.setDestinationCode(str);
                bus.setGroup(toInt(str));
            } else if (DIRECTION_TEXT.equals(lName)) {
                bus.setDestinationName(str);
            } else if (MINUTES.equals(lName)) {
                try {
                    bus.setMinutes(Integer.valueOf(str));
                } catch (final NumberFormatException e) {
                    bus.setMinutes(0);
                }
            } else if (ROUTE_ID.equals(lName)) {
                bus.setLine(str);
            } else if (VEHICLE_ID.equals(lName)) {
                bus.setId(str);
            }
        }
    }

    public ArrayList<Bus> getBuses() {
        return buses;
    }
}
